package com.ryskoo.customer.vendormanager.repository;

import com.ryskoo.common.hclconstant.EmployeeTypeEnum;
import com.ryskoo.customer.vendormanager.domain.VendorManagerPO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("customerVendorManagerJPAImpl")
public class VendorManagerJPAImpl extends BasicJPAImpl<VendorManagerPO> implements IVendorManagerRepository {

	/**
	 * 根据用户名查询管理员信息
	 * 
	 * @param userName
	 * @return
	 */
	@Override
	public VendorManagerPO getManagerByLoginName(String userName) {
		String hql = "select o from VendorManagerPO o where 1=1 and o.loginName =:loginName";
		Query q = this.getH4Session().createQuery(hql);
		q.setString("loginName", userName);
		Object object = q.uniqueResult();
		if (object != null) {
			return (VendorManagerPO) object;
		}
		return null;
	}

	/**
	 * 根据用户名查询管理员uuid
	 * 
	 * @param userName
	 * @return
	 */
	@Override
	public String getManagerUuidByLoginName(String userName) {
		String hql = "select o.uuid from VendorManagerPO o where 1=1 and o.loginName =:loginName";
		Query q = this.getH4Session().createQuery(hql);
		q.setString("loginName", userName);
		Object object = q.uniqueResult();
		if (object != null) {
			return (String) object;
		}
		return null;
	}

	/**
	 * 根据 商户uuid和是否超级管理员， 查找对应的员工
	 * 
	 * @param vendorUuids
	 * @param superManager
	 * @return
	 */
	@Override
	public List<VendorManagerPO> getVendorManagerByVendorUuid(List<String> vendorUuids, Boolean superManager) {
		String hql = "select o from VendorManagerPO o where o.vendorUuid in (:vendorUuids) ";
		if (superManager != null) {
			hql += "and o.superManager = :superManager";
		}
		Query q = this.getH4Session().createQuery(hql);
		q.setParameterList("vendorUuids", vendorUuids);
		if (superManager != null) {
			q.setBoolean("superManager", superManager);
		}
		return q.list();
	}

	@Override
	public List<VendorManagerPO> getManagerByVendorUuidAndType(String vendorUuid, int type) {
		String hql = "from VendorManagerPO o where 1=1 and o.vendorUuid =:vendorUuid and o.employeeType = :type and o.receiveNotification = 1";
		Query q = this.getH4Session().createQuery(hql);
		q.setString("vendorUuid", vendorUuid);
		q.setInteger("type",type);
		return q.list();
	}

	@Override
	public List<String> getUuidsByVendorUuidAndType(String vendorUuid, int type) {
		String hql = "select o.uuid from VendorManagerPO o where 1=1 and o.vendorUuid =:vendorUuid and o.employeeType = :type ";
		if(type==EmployeeTypeEnum.BOSS.getValue() || type == EmployeeTypeEnum.HEADCHEF.getValue()){
			hql += " and o.receiveNotification = 1 ";
		}
		Query q = this.getH4Session().createQuery(hql);
		q.setString("vendorUuid", vendorUuid);
		q.setInteger("type",type);
		return q.list();
	}

	@Override
	public void setNotReceiveNotificationToAll(String vendorUuid,int type,String managerUuid) {
		String hql = "update VendorManagerPO o set o.receiveNotification = false where o.vendorUuid = :vendorUuid and o.employeeType = :type ";
		if(StringUtils.isNotEmpty(managerUuid)){
			hql += " and o.uuid <> :managerUuid";
		}
		Query query = this.getH4Session().createQuery(hql);
		query.setString("vendorUuid",vendorUuid);
		query.setInteger("type",type);
		if(StringUtils.isNotEmpty(managerUuid)){
			query.setString("managerUuid",managerUuid);
		}
		query.executeUpdate();
	}
}
